package com.huika.renrench.http;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.huika.lib.encrypt.AESUtil;
import com.huika.lib.encrypt.Base64;
import com.huika.lib.encrypt.EncryptAlgorithm;
import com.huika.lib.encrypt.EncryptUtil;
import com.huika.lib.utils.DebugTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.config.Constant;

public class JSONEntity extends StringEntity{
	public static final String CT_APPLICATION_JSON = "application/json";
	
	public JSONEntity(List<BasicNameValuePair> paramsList) throws UnsupportedEncodingException {
		this(convertParamToJSON(paramsList).toString(), "UTF-8");
	}
	
	public JSONEntity(List<BasicNameValuePair> paramsList, String charset) throws UnsupportedEncodingException {
		super(convertParamToJSON(paramsList).toString(), charset);
		setContentType(CT_APPLICATION_JSON);
	}
	
	public JSONEntity(String json) throws UnsupportedEncodingException {
		this(json, "UTF-8");
	}

	public JSONEntity(String json, String charset) throws UnsupportedEncodingException {
		super(json, charset);
		setContentType(CT_APPLICATION_JSON);
	}
	
	public static JSONObject convertParamToJSON(List<BasicNameValuePair> paramsList) {
		int index = 0;
		JSONObject jsonObject = new JSONObject();
		try {
			for (BasicNameValuePair entry : paramsList) {
				String key = entry.getName();
				if (key.contains("password")||key.contains("userName")) {
					index ++;
				}
				if (!"modifyImageArray".equals(entry.getName())) {
					jsonObject.put(entry.getName(), entry.getValue());
				} else {
					JSONArray jsonArray = new JSONArray(entry.getValue());
					jsonObject.put(entry.getName(), jsonArray);
				}
			}
//			if (index==2) {
//				DebugTool.v("submit_json", "提交的json机密后:" + getLoginParams(jsonObject).toString());
//				return getLoginParams(jsonObject);
//			}
//				
//			DebugTool.v("submit_json", "提交的json机密后:" + getParams(jsonObject));
//			return getParams(jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DebugTool.v("submit_json", "提交的json:" + jsonObject.toString());
		return jsonObject;
	}
	/**
	 * @说明:RSA加密
	 * @author: zwy
	 */
	private static JSONObject getLoginParams(JSONObject jsonObject) throws JSONException {
		//DebugTool.v("submit_json", "提交的json加密前:" + jsonObject.toString());
		JSONObject obj = new JSONObject();
		obj.put("securityData", jsonObject);
		obj.put("securityType", "RSA");
		obj.put("userId", "0");
		return obj;
	}
	/**
	 * @说明:AES加密
	 * @author: zwy
	 */
	private static JSONObject getParams(JSONObject jsonObject) throws Exception {
		//DebugTool.v("submit_json", "提交的json加密前:" + jsonObject.toString());
		AESUtil ase = new AESUtil();
		String userId = GlobalApp.getInstance().getUserId();
		//String aesKey =( userId==null || TextUtils.isEmpty(userId) || "0".equals(userId)?Constant.AES_KEY:GlobalApp.userModel.getAesKey());
		JSONObject obj = new JSONObject();
		//obj.put("securityStr", EncryptUtil.testSymmEncry(jsonObject.toString(), Base64.decode(aesKey), EncryptAlgorithm.AES));
		obj.put("securityStr", ase.encrypt(jsonObject.toString(), Constant.AES_KEY));
		obj.put("securityType", "AES");
		obj.put("userId", userId);
		return obj;
	}
}
